﻿
Imports std
Imports xmlctrl
Imports rssctrl
Imports webctrl
Imports wfileis.data

Public Class rss
    Inherits apipage

    Public Const _pathidentity As String = network.pathSeperator + "rss"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Protected Overrides Sub addresponseHeaders(ByVal context As httpContext)
        assert(Not context Is Nothing, "context is nothing.")
#If RETIRED Then
        MyBase.addresponseHeaders(context)
#End If
        context.modifyResponseHeader(Net.HttpResponseHeader.ContentType, network.http.contentType.rssxml)
    End Sub

    Protected Overrides Function generateRoot(ByVal context As httpContext) As xmlcontrol
        Dim rtn As rsspage = Nothing
        rtn = context.createControl(New rsspage())
        rtn.title().text = context.string(stringId.RSS_TITLE)
        rtn.link().text = context.url().full

        Dim description As description = Nothing
        description = context.createControl(New description())
        description.text = applicationDescription()
        rtn.channel().addchild(description)

        Return rtn
    End Function

    Protected Overrides Sub processFailedTopic(ByVal context As httpContext, _
                                               ByVal root As xmlcontrol, ByVal key As String)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not root Is Nothing, "root is nothing.")

        Dim comment As comment = Nothing
        comment = context.createControl(New comment())
        comment.text = context.string(stringId.VIEW_FAILREADTOPIC) + " [" + key + "]"
        root.addchild(comment)
#If RETIRED Then
        'this logic will break the RSS standard.
        Dim item As item = Nothing
        item = context.createControl(New item())
        convertor.cast(Of rsspage)(root).channel().addchild(item)

        Dim title As title = Nothing
        title = context.createControl(New title())
        title.text = context.string(stringId.VIEW_FAILREADTOPIC) + " [" + key + "]"
        item.addchild(title)
#End If
    End Sub

    Protected Overridable Sub processInfoContent(ByVal context As httpContext, ByVal root As xmlcontrol, _
                                                 ByVal info As info, ByVal contentLength As Int64)
        Dim pv As posterViewer = Nothing
        pv = context.createControl(New posterViewer())
        Dim ds As posterViewer.datasource = Nothing
        ds = New posterViewer.datasource()
        ds.page = Nothing
        ds.text = info.text()
        ds.maxlength = contentLength
        ds.isIEUserAgent = context.isIEUserAgent()
        ds.promoteMultimediaContent = False
        pv.setData(ds)
        pv.style = "display:block;"
        root.addchild(pv)

        Dim idshow As idshow = Nothing
        idshow = context.createControl(New idshow())
        idshow.setData(info, " -- ")
        root.addchild(idshow)
    End Sub

    Private Sub processInfo(ByVal context As httpContext, ByVal root As xmlcontrol, _
                            ByVal key As String, ByVal info As info, _
                            ByVal pubDate As pubDate, _
                            ByVal contentLength As Int64)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not root Is Nothing, "root is nothing.")
        assert(Not pubDate Is Nothing, "pubDate is nothing.")

        If info Is Nothing Then
            Dim msg As xmlcontrol = Nothing
            msg = context.createControl(New xhtmlctrl.span())
            msg.text = context.string(stringId.VIEW_FAILREADINFO) + " [" + key + "]"
            root.addchild(msg)
        Else
            processInfoContent(context, root, info, contentLength)
            pubDate.text = toGMTTime(Date.Parse(info.time))
        End If
    End Sub

    Protected Overridable Function createDescriptionRoot(ByVal context As httpContext) As xmlcontrol
        Return context.createControl(New CDATA())
    End Function

    Protected Overridable Function createReplyInfo(ByVal context As httpContext) As xmlcontrol
        Dim rtn As xhtmlctrl.div = Nothing
        rtn = context.createControl(New xhtmlctrl.div())
        rtn.style = "margin-top: 1em;"
        Return rtn
    End Function

    Protected Overridable Function createInfoRoot(ByVal context As httpContext) As xmlcontrol
        Return context.createControl(New xhtmlctrl.div())
    End Function

    Private Function filldescription(ByVal context As httpContext, _
                                     ByVal description As description, _
                                     ByVal record As record, ByVal infos As vector(Of pointer(Of info)), _
                                     ByVal pubDate As pubDate, ByVal contentLength As Int64) As Boolean
        Dim root As xmlcontrol = Nothing
        root = createDescriptionRoot(context)
        description.addchild(root)

        Dim i As Int64
        For i = 0 To infos.size() - 1
            Dim ctr As xmlcontrol = Nothing
            If i > 0 Then
                ctr = createReplyInfo(context)
                ctr.text = context.string(stringId.REPLY_MESSAGE)
                root.addchild(ctr)
            End If
            ctr = createInfoRoot(context)
            root.addchild(ctr)
            processInfo(context, ctr, record(record.size() - infos.size() + i), +(infos(i)), pubDate, contentLength)
        Next

        Return True
    End Function

    Protected Overrides Sub processNoContent(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not root Is Nothing, "root is nothing.")
        Dim comment As comment = Nothing
        comment = context.createControl(New comment())
        comment.text = context.string(stringId.NO_MORE_CONTENT)
        root.addchild(comment)
    End Sub

    Protected Overrides Function processTopic(ByVal context As httpContext, ByVal root As xmlcontrol, _
                                              ByVal key As String, _
                                              ByVal record As record, ByVal infos As vector(Of pointer(Of info)), _
                                              ByVal contentLength As Int64) _
                                              As Boolean
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not root Is Nothing, "root is nothing.")
        assert(Not record Is Nothing, "record is nothing.")
        assert(Not infos Is Nothing AndAlso infos.size() > 0, "infos is nothing, or empty.")

        Dim rsspage As rsspage = Nothing
        rsspage = convertor.cast(Of rsspage)(root)

        Dim item As item = Nothing
        item = context.createControl(New item())
        rsspage.channel().addchild(item)

        Dim title As title = Nothing
        title = context.createControl(New title())
        title.text = key + " -- " + Convert.ToString(record.size() - 1) + context.string(stringId.REPLY_COUNT)
        item.addchild(title)

        Dim link As link = Nothing
        link = context.createControl(New link())
        link.text = strleft(context.url().full, _
                            strlen(context.url().full) - _
                            ifoper(Not isemptystring(context.url().queryString), _
                                   strlen(context.url().queryString) + strlen(network.questionMark), _
                                   0) - strlen(pathidentity()) + 1) + _
                    view.generateViewlink(key)
        item.addchild(link)

        Dim description As description = Nothing
        description = context.createControl(New description())
        item.addchild(description)

        Dim pubDate As pubDate = Nothing
        pubDate = context.createControl(New pubDate())
        item.addchild(pubDate)

        Dim source As source = Nothing
        source = context.createControl(New source())
        source.text = strmid(pathidentity(), strlen(network.pathSeperator))
        source.url = context.url().full
        item.addchild(source)

        filldescription(context, description, record, infos, pubDate, contentLength)

        Return True
    End Function
End Class
